<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script>
    $(function () {
        setCookStyle();
        let id = sessionStorage.getItem("productUpdateId");
        if (id !== "null") {
            getProductById(id);
            sessionStorage.setItem("productUpdateId", null);
        }
    });

    function setCookStyle() {
        let data = myAjax("cookStyleGet", "", "get");
        for (let i = 0; i < data.length; i++) {
            $("#productCookStyle").append("<option value='" + data[i].id + "'>" + data[i].name + "</option>");  //添加一项option
        }
    }

    function getProductById(id) {
        let data = myAjax("productDetailId", {id: id}, "get");
        $("#productId").val(data[0].id);
        $("#productName").val(data[0].name);
        $("#productMainMaterial").val(data[0].mainmaterial);
        $("#productContent").val(data[0].content);
        $("#productMarketPrice").val(data[0].marketPrice);
        $("#productPrice").val(data[0].price);
        $("#productimgUrl").val(data[0].imgUrl);
        //下拉菜单选中和该条信息栏目相同的type值
        $("#productCookStyle option[value=" + data[0].cookStyleId + "]").attr("selected", true);
    }

    function saveNews() {
        let id = $("#productId").val();
        let name = $("#productName").val();
        let mainmaterial = $("#productMainMaterial").val();
        let content = $("#productContent").val();
        let marketPrice = $("#productMarketPrice").val();
        let price = $("#productPrice").val();
        let imgUrl = $("#productimgUrl").val();
        let data = {
            method: id == "" ? "add" : "update",
            id: id,
            name: name,
            mainmaterial: mainmaterial,
            content: content,
            marketPrice: marketPrice,
            price: price,
            imgUrl: imgUrl,
            cookstyleid: $("#productCookStyle").get(0).value
        };
        myAjax("productUpdate", data, "get");
    }
</script>
<body>
<div><input type="text" value="" readonly id="productId"></div>
<div>名称：<input type="text" id="productName"></div>
<div>主料：<input type="text" id="productMainMaterial"></div>
<div>介绍：<input type="text" id="productContent"></div>
<div>市场价：<input type="text" id="productMarketPrice"></div>
<div>价格：<input type="text" id="productPrice"></div>
<div>图片：<input type="text" id="productimgUrl"></div>
<div>菜系<select id="productCookStyle"></select></div>
<div><input type="button" value="保存" onclick="saveNews()"></div>
</body>
</html>